package com.svo.weitu.model.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.svo.weitu.model.entity.ListEntity;

public class FileData {
	private Context context;
	private DbHelper dbHelper;
	public FileData(Context context) {
		this.context = context;
		dbHelper = DbHelper.getInstance(context);
	}
	
	public long insert(String name,int parId) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("parId", parId);
		return db.insert("file", null, values);
	}
	public void insert(List<String> names,int parId) {
		for (String title : names) {
			insert(title, parId);
		}
	}
	public List<ListEntity> queryAll(int i) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from file where parId = ?";
		Cursor cursor = db.rawQuery(sql,new String[]{i+""});
		List<ListEntity> entities = new ArrayList<ListEntity>();
		ListEntity entity;
		while (cursor.moveToNext()) {
			entity = new ListEntity(context);
			entity.setTitle(cursor.getString(cursor.getColumnIndex("name")));
			entity.setParId(cursor.getInt(cursor.getColumnIndex("parId")));
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entities.add(entity);
		}
		return entities;
	}
	/**
	 * 得到图片的名字
	 * @param i
	 * @return
	 */
	public List<String> queryAll2(int i) {
		List<String> titles = new ArrayList<String>();
		List<ListEntity> entities = queryAll(i);
		for (ListEntity listEntity : entities) {
			titles.add(listEntity.getTitle());
		}
		return titles;
	}
}
